package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeysUtil;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

//@Component //先不开
public class RedisDateInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("------------Redis初始化开始--------------");
        List<StrategyStatisVO> list = new ArrayList<>();
        List<StrategyDetail> strategyDetails = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : strategyDetails) {

            if(strategyStatisVOService.isDetailDateExist(detail.getId())){
                //true表示已存在,没必要再初始化一次
                continue;
            }
            vo = new StrategyStatisVO();
            //相同属性复制
            BeanUtils.copyProperties(detail,vo);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            vo.setTitle(detail.getTitle());
            vo.setIsabroad(detail.isIsabroad());
            //阅读
            vo.setViewnum(detail.getViewnum());
            //回复
            vo.setReplynum(detail.getReplynum());
            //收藏
            vo.setFavornum(detail.getFavornum());
            //分享
            vo.setSharenum(detail.getSharenum());
            //点赞
            vo.setThumbsupnum(detail.getThumbsupnum());
            //初始化vo对象
            strategyStatisVOService.setStatisVONum(vo);
            //初始热门攻略推荐排行
            strategyStatisVOService.setHotStrategyCommendCds(vo);
            //初始国内外攻略推荐排行
            strategyStatisVOService.setHotCountryCommendCds(vo);
        }



        System.out.println("------------Redis初始化结束--------------");











    }
}
